<?php


declare(strict_types=1);

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Utils\ApiUtils;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Extend\Wx\Wxlogin as WxloginExtend;
use App\Server\CurlApi;
use App\Extend\Wx\Wxinfo;

/**
 * @Controller()
 */
class TemplateController extends AbstractController
{
    /**
     * 发送订阅消息
     * POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/wxinfo", methods="get")
     */
    public function wxinfo(){
        $result = make(Wxinfo::class)->daily_summary();
        return $result;
    }

    /**
     * 发送订阅消息
     * POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/send", methods="get")
     */
    public function send(RequestInterface $request){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$data;
        $json_array = [
              "touser"=> "o81z64uBw3ucvh-W-S5yG3WyL5Uw",
              "template_id"=> "3kx0wQoSbfAqPv7tgJAmKb0bcIIVecCGcns8AAUkAcw",
              "page"=> "index",
              "miniprogram_state"=>"developer",
              "lang"=>"zh_CN",
              "data"=>[
                  "character_string2" => [
                      "value"=>"339208499"
                  ],
                  "thing6" => [
                      "value"=>"TIT创意园"
                  ],
                  "time8" => [
                      "value"=>"2015年01月05日"
                  ],
                  "thing9" => [
                      "value"=>"广州市新港中路397号"
                  ],
              ]
        ];
        $result = make(CurlApi::class)->jsonpost($url,$json_array);
        return $result;


    }

    /**
     * 获取模板标题下的关键词列表
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/get_pub_template_key_words_by_id", methods="get")
     */
    public function getPubTemplateKeyWordsById(){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token='.$data;
        var_dump($url);
        $tid = '8040';
        $json_data = ['tid'=>99];
        $url_post = make(CurlApi::class)->jsonget($url,$json_data);
        return $url_post;
    }

    /**
     * 获取小程序账号的类目 ==>ok
     * GET https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=ACCESS_TOKEN
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/get_category", methods="get")
     */
    public function getCategory(){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token='.$data;
        return make(CurlApi::class)->get($url);
    }

    /**
     * 组合模板并添加至帐号下的个人模板库 ==>ok
     * POST https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=ACCESS_TOKEN
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/add_template", methods="get")
     */
    public function addTemplate(){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$data;
        // $retain_result = make(CurlApi::class)->jsonpost($retain_url,$wx_request);
        $json_request = ['tid'=>2298,'kidList'=>[1,2],'sceneDesc'=>'测试数据'];
        $url_request = make(CurlApi::class)->jsonpost($url,$json_request);
        return $url_request;
    }

    /**
     * 获取当前帐号下的个人模板列表 ==> ok
     * Abell 2020/5/13
     * @RequestMapping(path="/abell/template/get_template_list", methods="get")
     */
    public function getTemplateList(RequestInterface $request){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        // 获取当前帐号下的个人模板列表
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token='.$data;
        $template_list = make(CurlApi::class)->get($url);
        if($template_list['errorCode']){
            return $this->response->json(ApiUtils::send(400,'微信请求数据错误',$template_list,1));
        }else{
            // 处理data的json数据
            $template_list_data = json_decode($template_list['data'],true);
            return $this->response->json(ApiUtils::send(0,'ok',$template_list_data,1));
        }
    }

}
